package cn.tedu.boot.demo.controller;

import cn.tedu.boot.demo.pojo.dto.AdminAddNewDTO;
import cn.tedu.boot.demo.pojo.dto.AdminLoginDTO;
import cn.tedu.boot.demo.pojo.vo.AdminSimpleVO;
import cn.tedu.boot.demo.service.IAdminService;
import cn.tedu.boot.demo.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "管理员管理模块")
@RestController
@RequestMapping(value = "/admins", produces = "application/json; charset=utf-8")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    // 注意：暂时使用@RequestMapping，不要使用@PostMapping，以便于直接在浏览器中测试
    // http://localhost:8080/admins/add-new?username=root&password=1234
    @ApiOperation("增加管理员")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated AdminAddNewDTO adminAddNewDTO) {
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 900)
    @PostMapping("/login")
    public JsonResult<AdminSimpleVO> login(@Validated AdminLoginDTO adminLoginDTO) {
        AdminSimpleVO adminSimpleVO = adminService.login(adminLoginDTO);
        return JsonResult.ok(adminSimpleVO);
    }

}
